package day12;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.ThreadLocalRandom;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class homework01 {
    public static void main(String []args) throws Exception {
        String strXingURL="https://hanyu.baidu.com/shici/detail?from=kg1&highlight=&pid=0b2f26d4c0ddb3ee693fdb1137ee1b0d&srcid=51369";
        String womanstrMingURL="https://zhuanlan.zhihu.com/p/148059482";
        String manstrMingURL="https://zhuanlan.zhihu.com/p/362427768";
        String womanstrMing=WatchURL(womanstrMingURL);
        String manstrMing=WatchURL(manstrMingURL);
        String strXing=WatchURL(strXingURL);
        ArrayList<String> xinglist=LIST(strXing,"(.{4})(，|。)",1);
        ArrayList<String> manminglist=LIST(manstrMing,"(\\W.\\s){3}..",0);
        ArrayList<String> womanminglist=LIST(womanstrMing,"(\\W.\\s){3}..",0);
        ArrayList<String> manminglist2=splitlist(manminglist," ");
        ArrayList<String> womanminglist2=splitlist(womanminglist," ");
        ArrayList<String> xinglist2=splitlist(xinglist,"");
        ArrayList<String> womannameslist=namesplicing(xinglist2,womanminglist2,50);
        ArrayList<String> mannameslist=namesplicing(xinglist2,manminglist2,50);
        putfile(womannameslist,"womannames.txt","女");
        putfile(mannameslist,"mannames.txt","男");
        }

    private static void putfile(ArrayList<String> nameslist, String file,String sex) throws IOException {
        FileWriter fw= new FileWriter(file);
        BufferedWriter bw=new BufferedWriter(fw);
        for (String s : nameslist) {
            if(sex.equals("男")){
                ThreadLocalRandom tlr=ThreadLocalRandom.current();
                String age= String.valueOf(tlr.nextInt(18,27));
                bw.write(s+"-"+sex+"-"+age);
                bw.newLine();
            }else if(sex.equals("女")){
                ThreadLocalRandom tlr=ThreadLocalRandom.current();
                String age= String.valueOf(tlr.nextInt(18,25));
                bw.write(s+"-"+sex+"-"+age);
                bw.newLine();
            }
        }
        bw.close();
    }

    private static ArrayList<String> namesplicing(ArrayList<String> xinglist, ArrayList<String> minglist,int num) {
        Set<String> set=new HashSet<>();
        for (int i=0;i<num;i++) {
            Collections.shuffle(xinglist);
            Collections.shuffle(minglist);
            set.add(xinglist.get(0)+minglist.get(0));
        }
        ArrayList<String> list = new ArrayList<>(set);
        return list;
    }

    private static ArrayList<String> splitlist(ArrayList<String> list1,String regex) {
        ArrayList<String> list=new ArrayList<>();
        boolean flag=false;
        for (String str: list1) {
            String[] split = str.split(regex);
            if(str.startsWith("万俟")){
                flag=true;
            }
            if(flag){
                String s1 = split[0] + split[1];
                list.add(s1);
                String s2=split[2]+split[2];
                list.add(s2);
            }else {
                for (String s: split) {
                    list.add(s);
                }
            }
        }
        return list;
    }
    private static ArrayList<String> LIST(String strXing,String regex,int index) {
        ArrayList<String> list=new ArrayList<>();
        String str = strXing;
        String pattern = regex;
        Pattern r = Pattern.compile(pattern);
        Matcher m = r.matcher(str);
        while (m.find()){
            String xing=m.group(index);
            list.add(xing);
        }
        return list;
    }

    public static String WatchURL(String strURL) throws Exception{
        InputStream in = new URL( strURL ). openStream();
        BufferedReader reader = new BufferedReader( new InputStreamReader( in ) );
        StringBuffer sb=new StringBuffer();
        String b;
        while((b=reader.readLine())!=null){
            sb.append(b);
        }
        reader.close();
        return sb.toString();
    }
}
